package com.xcity.game.io;

import java.util.LinkedList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.xcity.db.entity.Player;
import com.xcity.game.adventure.model.Adventure;
import com.xcity.game.adventure.model.Foe;
import com.xcity.game.assist.AssistBuild;
import com.xcity.game.assist.AssistManager;
import com.xcity.game.buildv2.BuildManagerV2;
import com.xcity.game.buildv2.BuildV2;
import com.xcity.game.buildv2.BuildV2Part;
import com.xcity.game.conquests.Conquest;
import com.xcity.game.conquests.Conquests;
import com.xcity.game.task.Task;
import com.xcity.game.task.TaskManager;

import naga.x.util.CRC;

public class SerializerUtils {

	public static int hashCode(Serializer serializer) {
		SerializeStream out = new SerializeStream(1024);
		serializer.write(out);
		return CRC.crc32int(out.buf(), 0, out.size());
	}
	
	public static void serialize(Serializer serializer, SerializeStream out) {
		// do nothing
	}
	
	public static void serialize(Player player, SerializeStream out) {
		// the player basic info
		out.writeLong(player.getId());
		out.writeInt(player.getLevel());
		out.writeLong(player.getExp());
		out.writeLong(player.getGold());
		out.writeInt(player.getStone());
		out.writeInt(player.getStrength());
		out.write(JSON.toJSONBytes(player.getPool()));
		// the adventure
		player.getAdventure().write(out);
		// the buildings v2
		player.getBuildings2().write(out);
		// the tasks
		player.getTasks().write(out);
		// the conquests
		player.getConquests().write(out);
		// the assistbuild
		player.getAssists().write(out);
	}
	
	public static void serialize(Adventure adv, SerializeStream out) {
		if (adv == null) {
			out.writeInt(0);
			return;
		}
		LinkedList<Foe> foes = adv.getFoes();
		if (foes == null || foes.isEmpty()) {
			out.writeInt(0);
			return;
		}
		out.writeInt(foes.size());
		for (Foe foe : foes) {
			out.writeLong(foe.getId());
			out.write(foe.getCause());
			out.writeInt(foe.getType());
			out.writeInt(foe.getCount());
			out.writeLong(foe.getTime() != null ? foe.getTime().getTime() : 0);
		}
	}
	
	public static void serialize(BuildManagerV2 buildings, SerializeStream out) {
		if (buildings == null) {
			out.writeInt(0);
			return;
		}
		List<BuildV2> list = buildings.getBuildings();
		int size = buildings.size();
		out.writeInt(size);
		for (int i = 0; i < size; i++) {
			BuildV2 building = list.get(i);
			out.writeInt(building.getId());
			out.writeEnum(building.getState());
			BuildV2Part[] parts = building.getParts();
			for (BuildV2Part part : parts) {
				out.writeInt(part.getIndex());
				out.writeInt(part.getLevel());
				out.write(part.getState());
				out.writeInt(part.getMaxLevelBeforeBroken());
			}
			out.writeLong(building.getInvaderId());
		}
	}
	
	public static void serialize(TaskManager taskManager, SerializeStream out) {
		if (taskManager == null) {
			out.writeInt(0);
			return;
		}
		List<Task> tasks = taskManager.getTasks();
		int size = tasks.size();
		out.writeInt(size);
		for (int i = 0; i < size; i++) {
			Task task = tasks.get(i);
			out.writeInt(task.getId());
			out.writeEnum(task.getState());
			out.writeInt(task.getProgress());
//			out.writeLong(task.getCreateTime());
			out.write(JSON.toJSONBytes(task.getArchive()));
		}
	}
	
	public static void serialize(Conquests conquests, SerializeStream out) {
		if (conquests == null) {
			out.writeInt(0);
			return;
		}
		List<Conquest> list = conquests.getConquests();
		int size = list != null ? list.size() : 0;
		out.writeInt(size);
		for (int i = 0; i < size; i++) {
			Conquest c = list.get(i);
			out.writeLong(c.getOwnerId());
			out.writeInt(c.getId());
			long[] eids = c.getEmployees();
			out.write(eids.length);
			for (long eid : eids) {
				out.writeLong(eid);
			}
			out.writeLong(c.getLastStartMakeMoneyTime());
		}
	}
	
	public static void serialize(AssistManager assists, SerializeStream out){
		if(assists == null){
			out.writeInt(0);
			return;
		}
		List<AssistBuild> list = assists.getAssistBuildList();
		int size = list != null? list.size() : 0;
		for(int i = 0; i < size; i++){
			AssistBuild assbuild = list.get(i);
			out.writeLong(assbuild.getOwnerId());
			out.writeInt(assbuild.getId());
			long[] eids = assbuild.getEmployees();
			out.write(eids.length);
			for(long eid : eids){
				out.writeLong(eid);
			}
			out.writeLong(assbuild.getLastStartMakeMoneyTime());
			out.writeLong(assbuild.getAssistStartTime());
		}
		
	}
}
